package httpCtrler

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/go-xorm/xorm"
	st "kjjcgroup_server/model"
	sv "kjjcgroup_server/service"
	"net/http"
	"time"
)

func CertRouter(router *gin.Engine, routerAuth *gin.RouterGroup) {
	router.GET("/cert", cert)
	LogMap["/cert"] = "查询单位"
	routerAuth.PUT("/certAdd", certAdd)
	LogMap["/certAdd"] = "添加单位"
	routerAuth.PUT("/certEdit", certEdit)
	LogMap["/certEdit"] = "编辑单位"
	routerAuth.DELETE("/certDel", certDel)
	LogMap["/certDel"] = "删除单位"
}

func cert(c *gin.Context) {
	CertId := c.Query("CertId")
	CertName := c.Query("cert")
	pageIdx, pageCnt := Page(c)
	if pageIdx == -1 {
		pageIdx = 0
	}
	if pageCnt == -1 {
		pageCnt = 10
	}
	mysql, err := sv.GetEngine()
	if err != nil {
		c.JSON(409, &MsgReturn{Msg: "Open DataBase Failed", Data: nil})
		return
	}
	defer func(mysql *xorm.Engine) {
		err := mysql.Close()
		if err != nil {
			return
		}
	}(mysql)
	mysql.ShowSQL(true)
	var collArr []*st.CertMod
	var collArr1 []*st.CertMod
	var count int64
	if CertId != "" {
		err = mysql.Where("cert_id=?", CertId).Find(&collArr)
		c.JSON(http.StatusOK, &MsgReturn{Msg: "OK", Data: collArr})
		return
	} else if CertName != "" {
		err = mysql.Where("cert_name = ?", CertName).Or("cert_num = ?", CertName).Find(&collArr)
		c.JSON(http.StatusOK, &MsgReturn{Msg: "OK", Data: collArr})
		return
	} else {
		err = mysql.Limit(pageCnt, pageIdx*pageCnt).OrderBy("update_time desc").Find(&collArr)
		count, _ = mysql.FindAndCount(&collArr1)
	}
	if err != nil {
		fmt.Println(err)
		c.JSON(409, &MsgReturn{Msg: "failed", Data: nil})
		return
	}
	rtnDt := rtnData(count, pageCnt, pageIdx, collArr)
	c.JSON(http.StatusOK, &MsgReturn{Msg: "OK", Data: rtnDt})
}

func certAdd(c *gin.Context) {
	resDt := &st.CertMod{}
	err := c.BindJSON(resDt)
	if err != nil {
		return
	}
	fmt.Printf("JhVar %+v \r\n", resDt)

	mysql, err := sv.GetEngine()
	//	mysql.ShowSQL(true)
	if err != nil {
		c.JSON(409, "")
		return
	}
	defer func(mysql *xorm.Engine) {
		err := mysql.Close()
		if err != nil {

		}
	}(mysql)
	sess := mysql.NewSession()
	defer sess.Close()
	err = sess.Begin()
	if err != nil {
		return
	}
	t := time.Now()
	t = time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), 0, time.Local)
	_, err = sess.Cols("cert_name", "cert_num", "cert_img", "cert_con", "create_time").InsertOne(&st.CertMod{CertName: resDt.CertName, CertNum: resDt.CertNum, CertImg: resDt.CertImg, CertCon: resDt.CertCon, CreateTime: t})
	if err != nil {
		err := sess.Rollback()
		if err != nil {
			return
		}
	} else {
		err = sess.Commit()
	}
	if err != nil {
		c.JSON(409, err.Error())
		return
	}
	c.JSON(http.StatusOK, &MsgReturn{Msg: "OK", Data: resDt.CertName + "添加成功!"})
}

func certEdit(c *gin.Context) {
	resDt := &st.CertMod{}
	err := c.BindJSON(resDt)
	if err != nil {
		return
	}
	fmt.Printf("JhVar %+v \r\n", resDt)

	mysql, err := sv.GetEngine()
	// mysql.ShowSQL(true)
	if err != nil {
		c.JSON(409, "")
		return
	}
	defer func(mysql *xorm.Engine) {
		err := mysql.Close()
		if err != nil {

		}
	}(mysql)
	sess := mysql.NewSession()
	defer sess.Close()
	err = sess.Begin()
	if err != nil {
		return
	}
	t := time.Now()
	t = time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), 0, time.Local)
	_, err = sess.Where("cert_id=?", resDt.CertId).Cols("cert_name", "cert_num", "cert_img", "cert_con").Update(&st.CertMod{CertName: resDt.CertName, CertNum: resDt.CertNum, CertImg: resDt.CertImg, CertCon: resDt.CertCon})
	if err != nil {
		err := sess.Rollback()
		if err != nil {
			return
		}
	} else {
		err = sess.Commit()
	}
	if err != nil {
		c.JSON(409, err.Error())
		return
	}
	c.JSON(http.StatusOK, &MsgReturn{Msg: "OK", Data: "id:" + resDt.CertName + " 编辑成功!"})
}

func certDel(c *gin.Context) {
	CertId := c.Query("CertId")
	mysql, err := sv.GetEngBom()
	if err != nil {
		fmt.Println(err)
		c.JSON(409, &MsgReturn{Msg: "Failed", Data: nil})
		return
	}
	defer func(mysql *xorm.Engine) {
		err := mysql.Close()
		if err != nil {
			c.JSON(409, "")
			return
		}
	}(mysql)
	_, err = mysql.Where("cert_id=?", CertId).Delete(&st.CertMod{})
	if err != nil {
		fmt.Println(err)
		c.JSON(409, &MsgReturn{Msg: "Failed", Data: nil})
		return
	}
	c.JSON(http.StatusOK, &MsgReturn{Msg: "OK", Data: "id:" + CertId + " 删除成功!"})
}
